package com.hj.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.reggie.common.R;
import com.hj.reggie.domain.SetmealDish;
import com.hj.reggie.dto.Ipml.SetmealDtoImpl;
import com.hj.reggie.dto.SetmealDto;
import com.hj.reggie.service.SetmealDishService;
import com.hj.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 37460
 * 套餐管理  Controller
 */
@RequestMapping("/setmeal")
@Slf4j
@RestController
public class SetmealController {

    private final SetmealService setmealService;

    private final SetmealDishService setmealDishService;

    private final SetmealDtoImpl setmealDtoImpl;

    public SetmealController(SetmealService setmealService, SetmealDishService setmealDishService, SetmealDtoImpl setmealDtoImpl) {
        this.setmealService = setmealService;
        this.setmealDishService = setmealDishService;
        this.setmealDtoImpl = setmealDtoImpl;
    }


    /**
     * @param setmealDto 接受setmealDto对象数据
     * @return  新增成功
     */
    @PostMapping
    public R<String> saveSetmealAndDish(@RequestBody SetmealDto setmealDto) {
        //保存Setmeal数据
        setmealService.save(setmealDto);
        //获取Setmeal中CategoryId
        List<SetmealDish> collect = setmealDto.getSetmealDishes().stream().peek(setmealDish -> {
            //塞入setmealDish表中SetmealId
            String SetmealId = setmealDto.getCategoryId().toString();
            setmealDish.setSetmealId(SetmealId);
        }).collect(Collectors.toList());
        //批量setmealDish 保存
        setmealDishService.saveBatch(collect);
        return R.success("新增成功");
    }

    /**
     * @param page     页数
     * @param pageSize 页数展示条数
     * @param name     根据name like 查询相关满足条件的
     * @return 返回R<Page<SetmealDto>>对象数据
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> pageSetmeal(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = setmealDtoImpl.pageSetmeal(page, pageSize, name);
        return R.success(setmealDtoPage);
    }


}
